package de.breitbeil.jbooking.richclient.form.binding.comboboxrenderer;

import java.util.Comparator;
import java.util.Locale;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.comparator.NullSafeComparator;

abstract public class AbstractComboboxRenderer implements ApplicationContextAware, ComboBoxRenderer {

    private ApplicationContext context;
    private Comparator<Object> comparator;
    
    public AbstractComboboxRenderer() {
        comparator = new NullSafeComparator(String.CASE_INSENSITIVE_ORDER, false);
    }
    
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        this.context = context;
    }
    
    public String getMessage(String messageCode) {
        return context.getMessage(messageCode, null, messageCode, Locale.getDefault());
    }
    
    public String getMessage(String messageCode, Object[] args) {
        return context.getMessage(messageCode, args, messageCode, Locale.getDefault());
    }

    abstract public String getString(Object item);
    
    public String getValue(Object item) {
        return getString(item);
    }
    
    public int compare(Object o1, Object o2) {
        return comparator.compare(getString(o1), getString(o2));
    }
}
